package com.iakuil.app.service.impl;

import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.iakuil.app.common.core.exception.BusinessException;
import com.iakuil.app.domain.entity.Book;
import com.iakuil.app.domain.entity.Press;
import com.iakuil.app.repository.dao.PressMapper;
import com.iakuil.app.service.PressService;
import com.iakuil.app.service.base.CacheableServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 出版社 服务实现类
 *
 * @author Kai
 * @since 2023-12-06
 */
@Service
public class PressServiceImpl extends CacheableServiceImpl<PressMapper, Press> implements PressService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByIds(Long... ids) {
        for (Long id : ids) {
            this.remove(id);
        }

        return super.removeByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean remove(Long id) {
        // 商城书籍可以没有作者，但是不能没有出版社，所以删除前必须做强校验。
        // 查询引用关系时，经常出现上游服务依赖下游服务的情况，可以通过MP的Db类避免循环依赖
        Book query = new Book();
        query.setPressId(id);
        if (Db.count(query) > 0) {
            throw new BusinessException("出版社已被引用，无法删除");
        }
        return super.remove(id);
    }
}
